package cn.edu.lzu.service.impl;


import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.dao.CityMapper;
import cn.edu.lzu.dao.ExaminerMapper;
import cn.edu.lzu.dao.UnitMapper;
import cn.edu.lzu.entity.City;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.Unit;
import cn.edu.lzu.service.UnitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class UnitServiceImpl implements UnitService {
    @Resource
    private UnitMapper unitMapper;
    @Resource
    private ExaminerMapper examinerMapper;
    @Resource
    private CityMapper cityMapper;

    @Override
    public DataPage selectPageUnit(int currentPageNum, int pageSize) {
        int totalCount = unitMapper.selUnitCount();
        List<Unit> unitList = unitMapper.selPageUnit((currentPageNum - 1) * pageSize, pageSize);
        DataPage unitPage = new DataPage();
        unitPage.setCode(0);
        unitPage.setData(unitList);
        unitPage.setTotalCount(totalCount);
        unitPage.setMsg("成功");
        return unitPage;
    }

    @Override
    public long selectMaxUnitId() {
        Object maxUnitId = unitMapper.selMaxUnitId();
        if (maxUnitId != null) {
            return (long) maxUnitId;
        } else {
            return 0;
        }
    }

    @Override
    public int insertUnit(Unit unit) {
        return unitMapper.insUnit(unit);
    }

    @Override
    public ResponseResult updateUnitDisabled(boolean disabled, long unitId) {
        int result=unitMapper.updDisabled(disabled, unitId);
        ResponseResult responseResult=new ResponseResult();
        String msg="";
        if(disabled)
            msg=msg+"启用";
        else
            msg=msg+"停用";
        if(result==1){
            responseResult.setCode(1);
            responseResult.setMsg(msg+"成功");
        }else{
            responseResult.setCode(0);
            responseResult.setMsg(msg+"失败");
        }
        return responseResult;
    }

    @Override
    public int deleteUnitById(long unitId) {
        return unitMapper.delUnitById(unitId);
    }

    @Override
    public int deleteBatchUnitsByIds(String unitIds) {
        return unitMapper.delBatchUnitsByIds(unitIds);
    }

    @Override
    public int updateBatchEnabledUnitsByIds(String unitIds) {
        return unitMapper.updBatchEnabledUnitsByIds(unitIds);
    }

    @Override
    public int updateBatchDisabledUnitsByIds(String unitIds) {
        return unitMapper.updBatchDisabledUnitsByIds(unitIds);
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 注册单位功能，将信息写入unit，同时在examiner中写入初始账号。
     * @Date: 2019/11/28 16:10
     * @Param: [unit]
     * @Return: boolean
     **/
    @Override
    public ResponseResult registeUnit(Unit unit) {
        System.out.println("unit = " + unit);
        ResponseResult responseResult=new ResponseResult();
        List<Unit> unitList=unitMapper.selUnitByName(unit.getUnitName());
        System.out.println(unitList.size());
        if(unitList!=null && unitList.size()>0){
            responseResult.setCode(0);
            responseResult.setMsg("注册失败，该单位名称已存在！");
            return responseResult;
        }

        Examiner examiner=new Examiner();
        //examiner初始账号Id
        String examinerIdHead = ("" + unit.getUnitId()).substring(0, 6);
        long examinerId=Long.valueOf(examinerIdHead+"000000");
        examiner.setExaminerId(examinerId);
        //examiner初始账号密码（000000，用Md5加密）
        /*examiner.setPassword(new String(DigestUtils.md5("000000"))); 暂时先不做md5加密*/
        examiner.setPassword("000000");
        //examiner初始账号unitId
        examiner.setUnitId(unit.getUnitId());
        //examiner账号注册时间
        examiner.setRegisteTime(new Timestamp(System.currentTimeMillis()));
        //给初始账号命名为‘初始账号’
        examiner.setExaminerName("初始账号");
        //设置初始账号为启用状态
        examiner.setStatus(true);

        //写入unit
        int unitResult=unitMapper.insUnit(unit);
        //写入examiner
        int examinerResult=examinerMapper.insExaminer(examiner);

        //result：最终结果；unit和examiner都写入成功，result为true，否则为false
        boolean result;
        if(unitResult==1 && examinerResult==1){
            responseResult.setCode(1);
            responseResult.setMsg("注册成功！");
        }
        else{
            responseResult.setCode(0);
            responseResult.setMsg("注册失败！");
        }
        return responseResult;
    }

    @Override
    public Unit selectUnitById(long unitId) {
        return unitMapper.selUnitById(unitId);
    }

    @Override
    public List<Unit> selectUnitByName(Unit unit) {
        return unitMapper.selUnitByName(unit.getUnitName());
    }

    @Override
    public List<Unit> getUnitsByProvinceId(Long provinceId) {
        List<City> cityList=cityMapper.selAllCityByProvinceId(provinceId);
        String cityIds="";
        for(City city:cityList){
            List<City> cityList1=cityMapper.selAllCountyByCityId(city.getCityId());
            for(City city1:cityList1){
                cityIds=cityIds+city1.getCityId()+",";
            }
        }
        List<Unit> unitList=unitMapper.selUnitsByCityId(cityIds.substring(0, cityIds.length()-1));
        return unitList;
    }

    @Override
    public List<Unit> getUnitsByAreaId(Long areaId) {
        List<Unit> unitList=unitMapper.selUnitsByCityId(areaId+"");
        return unitList;

    }

    @Override
    public List<Unit> getUnitsByCityId(Long cityId) {
        List<City> cityList=cityMapper.selAllCountyByCityId(cityId);
        String cityIds="";
        for(City city:cityList){
            cityIds=cityIds+city.getCityId()+",";
        }
        List<Unit> unitList=unitMapper.selUnitsByCityId(cityIds.substring(0, cityIds.length()-1));
        return unitList;
    }

}
